//
//  PayPickerViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 28..
//  Copyright 2011 Daum. All rights reserved.
//

#import "PayPickerViewController.h"
@interface PayPickerViewController(Util)
- (void)reloadShowLabel;
@end

@implementation PayPickerViewController
@synthesize payType = _payType;
@synthesize payPickerDataSource = _payPickerDataSource;
@synthesize payPickerView = _payPickerView;
@synthesize showLabel = _showLabel;
@synthesize applyBtn = _applyBtn;
@synthesize cancelBtn = _cancelBtn;

/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.payPickerDataSource = [[PayPickerDataSource alloc] init];
    }
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_applyBtn release];
    [_cancelBtn release];
    [_showLabel release];
    [_payPickerDataSource release];
    [_payPickerView release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    switch (_payType) {
        case PAY_CASH:
            self.title = @"현금 선택";
            [_showLabel setText:@"현금 > "];
            break;
        case PAY_CARD:
            self.title = @"카드 선택";
            [_showLabel setText:@"카드 > "];
            break;
        case PAY_BANK:
            self.title = @"은행 선택";
            [_showLabel setText:@"은행 > "];
            break;            
    }
    
    self.payPickerView.dataSource = _payPickerDataSource;
    _payPickerDataSource.payType = _payType;
    [_payPickerDataSource reloadData];
    [_payPickerView reloadComponent:0];
    if([_payPickerDataSource.payPickerArray count]>0){
//        [self.showLabel setText:[[_showLabel text] stringByAppendingString:_payPickerDataSource.payName]];
        [self.showLabel setText:[[_payPickerDataSource.payPickerArray objectAtIndex:[_payPickerView selectedRowInComponent:0]] objectForKey:@"title"]];
    }
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    self.applyBtn=nil;
    self.cancelBtn=nil;
    self.showLabel=nil;
    self.payPickerDataSource = nil;
    self.payPickerView = nil;
    [super viewDidUnload];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _payPickerDataSource.payType = _payType;
    [_payPickerDataSource reloadData];
    [_payPickerView reloadAllComponents];
    if([_payPickerDataSource.payPickerArray count]<=[_payPickerView selectedRowInComponent:0]){
        [_payPickerView selectRow:0 inComponent:0 animated:YES];
    }
    switch (_payType) {
        case PAY_CASH:
            self.title = @"현금 선택";
            [_showLabel setText:@"현금 > "];
            break;
        case PAY_CARD:
            self.title = @"카드 선택";
            [_showLabel setText:@"카드 > "];
            break;
        case PAY_BANK:
            self.title = @"은행 선택";
            [_showLabel setText:@"은행 > "];
            break;            
    }
    if([_payPickerDataSource.payPickerArray count]<=[_payPickerView selectedRowInComponent:0]){
        [_payPickerView selectRow:0 inComponent:0 animated:YES];
        [_resultDelegate applyResultSet:self resultDic:nil];
    }
    [self reloadShowLabel];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UIPickerViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [self reloadShowLabel];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //    NSLog(@"titleForRow");
	NSString *returnStr = @"";
    returnStr = [[_payPickerDataSource.payPickerArray objectAtIndex:row] objectForKey:@"title"];            
    return returnStr;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    //    NSLog(@"rowHeightForComponent");
	return 40.0;
}
#pragma mark -
#pragma mark Utils
- (void)reloadShowLabel{
    if([_payPickerDataSource.payPickerArray count]>0){
        switch (_payType) {
            case PAY_CASH:
                [_showLabel setText:@"현금 > "];
                break;
            case PAY_CARD:
                [_showLabel setText:@"카드 > "];
                break;
            case PAY_BANK:
                [_showLabel setText:@"은행 > "];
                break;            
        }
        if([_payPickerDataSource.payPickerArray count]>0){
            [self.showLabel setText:[[_showLabel text] stringByAppendingString:[[_payPickerDataSource.payPickerArray objectAtIndex:[_payPickerView selectedRowInComponent:0]] objectForKey:@"title"]]];
        }
    }else{
        [self.showLabel setText:@""];
    }

}
#pragma mark UIAlertViewDelegate mothods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    
    if(buttonIndex==0){
        [self openPayManage:nil];
    }
}

#pragma mark -
#pragma mark UIButtonDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)applyAct:(id)sender{
    NSLog(@"applyAct");
    if([_payPickerDataSource.payPickerArray count]==0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"웁스" message:@"목록이 없습니다.\n목록을 추가 하시겠습니까?." delegate:self cancelButtonTitle:nil otherButtonTitles:@"확인", @"아니오", nil];
        [alert show];	
        [alert release];        
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:[NSNumber numberWithInt:_payPickerDataSource.payType] forKey:@"payType"];
        switch (_payType) {
            case PAY_CASH:
                [dic setObject:[[_payPickerDataSource.payPickerArray objectAtIndex:[_payPickerView selectedRowInComponent:0]] objectForKey:@"cash_id"] forKey:@"payId"];
                break;
            case PAY_CARD:
                [dic setObject:[[_payPickerDataSource.payPickerArray objectAtIndex:[_payPickerView selectedRowInComponent:0]] objectForKey:@"card_id"] forKey:@"payId"];
                break;
            case PAY_BANK:
                [dic setObject:[[_payPickerDataSource.payPickerArray objectAtIndex:[_payPickerView selectedRowInComponent:0]] objectForKey:@"bank_id"] forKey:@"payId"];                
                break;            
        }        
        
        [dic setObject:[[_payPickerDataSource.payPickerArray objectAtIndex:[_payPickerView selectedRowInComponent:0]] objectForKey:@"title"] forKey:@"payName"];
        [dic setObject:_showLabel.text forKey:@"showName"];
        
        [_resultDelegate applyResultSet:self resultDic:dic];
        [[self navigationController] popViewControllerAnimated:YES];
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)cancelAct:(id)sender{
    NSLog(@"cancelAct");
    [[self navigationController] popViewControllerAnimated:YES];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)openPayManage:(id)sender{
    UIViewController *viewController;
    switch (_payType) {
        case PAY_CASH:
            viewController = [[[CashSettingViewController alloc] initWithNibName:@"CashSettingView" bundle:nil] autorelease];
            break;
        case PAY_CARD:
            viewController = [[[CardSettingViewController alloc] initWithNibName:@"CardSettingView" bundle:nil] autorelease];
            break;
        case PAY_BANK:
            viewController = [[[BankSettingViewController alloc] initWithNibName:@"BankSettingView" bundle:nil] autorelease];
            break;            
    }
    [[self navigationController] pushViewController:viewController animated:YES];                    
}
@end
